/**
 * Created by wangzilong on 2017/4/12.
 */
const test = 'http://test.520m.com.cn/api/';
const project = 'http://www.520m.com.cn/api/';
var serviceUrl = test;

function ImageFetch(serviceName, params, imgArr, token) {
    return new Promise(function (resolve, reject) {
        let formData = new FormData();
        for (var key in params){
            if(params[key]){
                formData.append(key, params[key]); 
            }
        }
        var str = '';
        for(var key in imgArr){
            if(imgArr[key]){
                // alert(imgArr[key])
                str += key+';';
                let file = {uri: imgArr[key], type: 'application/octet-stream', name: 'image.jpg'};
                formData.append(key, file);
            } 
        }
        var url = '';
        if(token){
            url = serviceUrl + serviceName + 'access-token=' + token;
        }else{
            url = serviceUrl + serviceName;
        }
        fetch(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'multipart/form-data;charset=utf-8',
            },
            body: formData,
        }).then((response) => response.json())
            .then((responseData)=> {
                resolve(responseData);
            })
            .catch((err)=> {
                reject(err);
            });
    });
}


module.exports = ImageFetch;

